/**
 * UI动态渲染
 * varstion 1.0.1
 */

(function($, doc) {
    var ms = {
        init: function(data) {
            ms.uiBind(data);
        },
        uiBind: function(data) {
            ms.switchInit();
            ms.inputInit();
            ms.radioInit();
            ms.checkboxInit();
            $(".mui-tscombobox").each(function(index, el) {
                var readys=el.getAttribute('data-ready');
                if(!readys){
                var idName = el.id;
                ms.comboBoxInit(idName, data);
                }

            });
        },
        radioInit: function() {
            $('.mui-radio').off('change', 'input');
            $('.mui-radio').on('change', 'input', function(e) {
                var parentBox = this.parentNode.parentNode;
                switchAllElements = parentBox.querySelectorAll('.mui-radio input');
                var valInput = parentBox.querySelector('.ts-valinput');
                $.each(switchAllElements, function(index, el) {
                    if (el.checked) {
                        valInput.value = index;
                    }
                });

            });
        },
        checkboxInit: function() {
            $('.mui-checkbox').off('change', 'input');
            $('.mui-checkbox').on('change', 'input', function(e) {
                var parentBox = this.parentNode.parentNode;
                switchAllElements = parentBox.querySelectorAll('.mui-checkbox input');
                var valInput = parentBox.querySelector('.ts-valinput');
                var slectVal = "";
                $.each(switchAllElements, function(index, el) {
                    if (el.checked) {
                        slectVal += index + '@'
                    }
                });
                slectVal = slectVal.substring(0, slectVal.length - 1)
                var valInput = parentBox.querySelector('.ts-valinput');
                valInput.value = slectVal;
            });

        },
        switchInit: function() {
            $('.mui-switch')['switch']();
            $('.mui-tsform').off('toggle', ".mui-content .mui-switch");
            $('.mui-tsform').on('toggle', ".mui-content .mui-switch", function(event) {
                var parentBox = this.parentNode.parentNode;
                switchAllElements = parentBox.querySelectorAll('.mui-switch');
                var slectVal = "";
                $.each(switchAllElements, function(index, el) {
                    var isActive = this.classList.contains('mui-active');
                    if (isActive) {
                        slectVal += index + '@'
                    }
                });
                slectVal = slectVal.substring(0, slectVal.length - 1)
                var valInput = parentBox.querySelector('.ts-valinput');
                valInput.value = slectVal;
            });
        },
        inputInit: function() {
            $($.classSelector('.input-row input')).input();
            $('.mui-tsform').off('change', '.ts-textbox');
            $('.mui-tsform').on('change', '.ts-textbox', function(e) {
                var parentBox = this.parentNode.parentNode;
                var valInput = parentBox.querySelector('.ts-valinput');
                valInput.value = this.value || this.text||"";
            })
        },
        comboBoxInit: function(id, data) {
            var userPicker = {};
            if (data[id][0].children && data[id][0].children.length) {
                userPicker = new $.PopPicker({
                    layer: 2
                });
            } else {
                userPicker = new $.PopPicker();
            }
            //普通示例
            userPicker.setData(data[id]);
            var showUserPickerButton = doc.getElementById(id);
            var userResult = doc.getElementById('userResult');
            showUserPickerButton.setAttribute("data-ready",true);
            showUserPickerButton.addEventListener('tap', function(event) {
                userPicker.show(function(items,index) {
                    var butTxt = "";
                    var valTxt="";
                    $.each(items, function(index, el) {
                        if (index == items.length - 1) {
                            butTxt += el.text;
                        } else {
                            butTxt += el.text + " . ";
                        }

                    });
                     $.each(index, function(index, el) {
                        if (index == items.length - 1) {
                            valTxt+=el;
                        } else {
                            valTxt += el + "#";
                        }

                    });
                    showUserPickerButton.innerText = butTxt;
                    //返回 false 可以阻止选择框的关闭
                    //return false;
                    var valInput = showUserPickerButton.parentNode.querySelector('.ts-valinput');
                    valInput.value = valTxt;
                });
            }, false);
        }
    }
    $.uiBind = function(data) {
        ms.init(data)
    }
    $.switchInit = function() {
        ms.switchInit();
    }
    $.inputInit = function() {
        ms.inputInit();
    }
    $.comboBoxInit = function(id, data) {
        ms.comboBoxInit();
    }
})(mui, document);